import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/demo_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:github_client_app/db/sqlite.dart';
import 'package:github_client_app/page/index_page.dart';
import 'package:github_client_app/provider/locale_provider.dart';
import 'package:github_client_app/provider/theme_provider.dart';
import 'package:github_client_app/router/routes.dart';
import 'package:github_client_app/services/http/dio_util.dart';
import 'package:github_client_app/services/tcp/tcp_util.dart';
import 'package:provider/provider.dart';
import 'package:bot_toast/bot_toast.dart';

main(List<String> args) async {
  WidgetsFlutterBinding.ensureInitialized();
  await MySqlite.forFeature();
  // TcpUtil.init();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key) {
    Routes.configureRoutes(); // 路由
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667), // 屏幕适配
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return MultiProvider(
            providers: [
              // 主题
              ChangeNotifierProvider(create: (_) => ThemeProvider()),
              // 语言
              ChangeNotifierProvider(create: (_) => LocaleProvider())
            ],
            child: Consumer2<ThemeProvider, LocaleProvider>(builder: (_,
                ThemeProvider themeProvider,
                LocaleProvider localeProvider,
                __) {
              // print("MultiProvider build");
              return FutureBuilder(
                  future: localeProvider.getLocale(),
                  builder: (context, snapshot) {
                    // print("FutureBuilder build");
                    return MaterialApp(
                      title: "Flutter project",
                      // 主题
                      theme: themeProvider.lightTheme,
                      darkTheme: themeProvider.darkTheme,
                      themeMode: themeProvider.getThemeMode(),
                      // 路由
                      onGenerateRoute: Routes.router.generator,
                      // 全局toast，loading
                      builder: BotToastInit(),
                      navigatorObservers: [BotToastNavigatorObserver()],
                      // 设置语言
                      locale: snapshot.data,
                      // 多语言
                      localizationsDelegates:
                          DemoLocalizations.localizationsDelegates,
                      // supportedLocales: DemoLocalizations.supportedLocales,
                      supportedLocales: const <Locale>[
                        Locale('en'),
                        Locale.fromSubtags(
                            languageCode: 'zh', scriptCode: 'Hans'),
                        Locale.fromSubtags(
                            languageCode: 'zh', scriptCode: 'Hant'),
                      ],
                      // debugShowCheckedModeBanner: false, // 去除右上角debug的标签
                      // showPerformanceOverlay: true, //显示性能标签
                      // checkerboardRasterCacheImages: true,
                      // showSemanticsDebugger: true, // 显示语义视图
                      // checkerboardOffscreenLayers: true, // 检查离屏渲染
                      // home: const IndexPage(),
                    );
                  });
            }));
      },
    );
  }
}
